package com.ruoyi.record.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.record.domain.RecordSalary;
import com.ruoyi.record.domain.vo.RecordContent1VO;
import com.ruoyi.record.domain.vo.RecordContent2VO;
import com.ruoyi.record.domain.vo.RecordContent3VO;
import com.ruoyi.record.domain.vo.RecordContent4VO;
import com.ruoyi.record.service.IRecordSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordSalaryContent;
import com.ruoyi.record.service.IRecordSalaryContentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工资详情Controller
 * 
 * @author ruoyi
 * @date 2023-02-12
 */
@Controller
@RequestMapping("/record/salary/content")
public class RecordSalaryContentController extends BaseController
{
    private String prefix = "record/salary/content";

    @Autowired
    private IRecordSalaryService recordSalaryService;

    @Autowired
    private IRecordSalaryContentService recordSalaryContentService;

    @GetMapping("/{salaryId}")
    public String content(@PathVariable(value = "salaryId") Long salaryId, ModelMap mmap)
    {
        mmap.put("salaryId", salaryId);
        return prefix + "/content";
    }

    /**
     * 查询工资详情列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordSalaryContent recordSalaryContent)
    {
        startPage();
        List<RecordSalaryContent> list = recordSalaryContentService.selectRecordSalaryContentList(recordSalaryContent);
        return getDataTable(list);
    }

    @Log(title = "工资详情", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Long salaryId, String salaryType, boolean updateSupport) throws Exception
    {
        List<RecordSalaryContent> contentList = new ArrayList<>();
        if(salaryType.equals("1")){
            ExcelUtil<RecordContent1VO> util = new ExcelUtil<RecordContent1VO>(RecordContent1VO.class);
            contentList = BeanUtils.copyList(util.importExcel(file.getInputStream()), RecordSalaryContent::new);
        }else if(salaryType.equals("2")){
            ExcelUtil<RecordContent2VO> util = new ExcelUtil<RecordContent2VO>(RecordContent2VO.class);
            contentList = BeanUtils.copyList(util.importExcel(file.getInputStream()), RecordSalaryContent::new);
        }else if(salaryType.equals("3")){
            ExcelUtil<RecordContent3VO> util = new ExcelUtil<RecordContent3VO>(RecordContent3VO.class);
            contentList = BeanUtils.copyList(util.importExcel(file.getInputStream()), RecordSalaryContent::new);
        }else if(salaryType.equals("4")){
            ExcelUtil<RecordContent4VO> util = new ExcelUtil<RecordContent4VO>(RecordContent4VO.class);
            contentList = BeanUtils.copyList(util.importExcel(file.getInputStream()), RecordSalaryContent::new);
        }
        String message = recordSalaryContentService.importContent(contentList, salaryId, salaryType, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate/{salaryType}")
    @ResponseBody
    public AjaxResult importTemplate(@PathVariable("salaryType") String salaryType)
    {
        if(salaryType.equals("1")){
            ExcelUtil<RecordContent1VO> util = new ExcelUtil<RecordContent1VO>(RecordContent1VO.class);
            return util.importTemplateExcel("在职员工工资表");
        }else if(salaryType.equals("2")){
            ExcelUtil<RecordContent2VO> util = new ExcelUtil<RecordContent2VO>(RecordContent2VO.class);
            return util.importTemplateExcel("退休职工工资表");
        }else if(salaryType.equals("3")){
            ExcelUtil<RecordContent3VO> util = new ExcelUtil<RecordContent3VO>(RecordContent3VO.class);
            return util.importTemplateExcel("铁路退休工资表");
        }else{
            ExcelUtil<RecordContent4VO> util = new ExcelUtil<RecordContent4VO>(RecordContent4VO.class);
            return util.importTemplateExcel("临时职工工资表");
        }
    }

    /**
     * 导出工资详情列表
     */
    @Log(title = "工资详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordSalaryContent recordSalaryContent)
    {
        List<RecordSalaryContent> list = recordSalaryContentService.selectRecordSalaryContentList(recordSalaryContent);
        ExcelUtil<RecordSalaryContent> util = new ExcelUtil<RecordSalaryContent>(RecordSalaryContent.class);
        return util.exportExcel(list, "工资详情数据");
    }

    /**
     * 新增工资详情
     */
    @GetMapping("/add/{salaryId}")
    public String add(@PathVariable("salaryId") Long salaryId, ModelMap mmap)
    {
        RecordSalary salary = recordSalaryService.selectRecordSalaryBySalaryId(salaryId);
        mmap.put("salary", salary);
        return prefix + "/add";
    }

    /**
     * 新增保存工资详情
     */
    @Log(title = "工资详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordSalaryContent recordSalaryContent)
    {
        recordSalaryContent.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordSalaryContentService.insertRecordSalaryContent(recordSalaryContent));
    }

    /**
     * 修改工资详情
     */
    @GetMapping("/edit/{contentId}")
    public String edit(@PathVariable("contentId") Long contentId, ModelMap mmap)
    {
        RecordSalaryContent recordSalaryContent = recordSalaryContentService.selectRecordSalaryContentByContentId(contentId);
        mmap.put("recordSalaryContent", recordSalaryContent);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资详情
     */
    @Log(title = "工资详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordSalaryContent recordSalaryContent)
    {
        recordSalaryContent.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordSalaryContentService.updateRecordSalaryContent(recordSalaryContent));
    }

    /**
     * 删除工资详情
     */
    @Log(title = "工资详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordSalaryContentService.deleteRecordSalaryContentByContentIds(ids));
    }

    /**
     * 校验员工是否存在工资记录
     */
    @PostMapping("/checkSalaryContent")
    @ResponseBody
    public AjaxResult checkSalaryContent(RecordSalaryContent recordSalaryContent)
    {
        boolean checkFlag = recordSalaryContentService.checkSalaryContent(recordSalaryContent);
        return checkFlag ? success() : error();
    }
}
